package com.renwei.yunlong.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.renwei.yunlong.R;

import java.util.ArrayList;

import io.github.xudaojie.qrcodelib.zxing.DensityUtils;

/**
 * Created by wangqi on 2017/12/18.
 */

@SuppressLint("AppCompatCustomView")
public class CircleTextImage extends ImageView {

    private int mCircleColor = -65536;
    private int mCircleTextColor = -1;
    private Paint mPaint = new Paint(1);
    private boolean useRandomBackgroundColor = false;
    private boolean mSubFirstCharacter = false;
    private String text;
    private Paint paintTextForeground;
    private Paint paintTextBackground;
    private static final float DEFAULT_TEXT_SIZE_RATIO = 0.4F;
    private float textSizeRatio = 0.3F;
    private Paint.FontMetrics fontMetrics;
    private int radius;
    private int centerX;
    private int centerY;

    public CircleTextImage(Context context) {
        super(context);
        this.init();
    }

    public CircleTextImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initAttr(context, attrs);
        this.init();
    }

    public CircleTextImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initAttr(context, attrs);
        this.init();
    }

    private void initAttr(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Circletextview);
            this.mCircleColor = typedArray.getColor(R.styleable.Circletextview_circle_color, -65536);
            this.mCircleTextColor = typedArray.getColor(R.styleable.Circletextview_circle_text_color, -1);
            this.useRandomBackgroundColor = typedArray.getBoolean(R.styleable.Circletextview_random_color, false);
            this.mSubFirstCharacter = typedArray.getBoolean(R.styleable.Circletextview_sub_first_character, false);
            typedArray.recycle();
        }
    }

    private void init() {
        this.paintTextForeground = new Paint();
        this.paintTextForeground.setColor(this.mCircleTextColor);
        this.paintTextForeground.setAntiAlias(true);
        this.paintTextForeground.setTextAlign(Paint.Align.CENTER);
        this.paintTextBackground = new Paint();
        this.paintTextBackground.setColor(this.mCircleTextColor);
        this.paintTextBackground.setAntiAlias(true);
        this.paintTextBackground.setStyle(Paint.Style.FILL);
        if (this.useRandomBackgroundColor) {
            this.mPaint.setColor(Color.parseColor(CircleTextImage.CircleTextImageUtil.getRandomColor()));
        } else {
            this.mPaint.setColor(this.mCircleColor);
        }

    }

    public void setText4CircleImage(String text) {
        if (this.mSubFirstCharacter) {
            this.text = CircleTextImage.CircleTextImageUtil.subFirstCharacter(text);
        } else {
            this.text = text;
        }

        this.invalidate();
    }

    public void setTextCircleColor(int color) {
        this.mPaint.setColor(color);
        this.invalidate();
    }

    private void drawText(Canvas canvas) {
        this.paintTextBackground.setColor(this.mCircleTextColor);
        canvas.drawCircle((float) this.centerX, (float) this.centerY, (float) this.radius, this.mPaint);
        canvas.drawText(this.text, 0, this.text.length(), (float) this.centerX, (float) this.centerY + Math.abs(this.fontMetrics.top + this.fontMetrics.bottom) / 2.0F, this.paintTextForeground);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        short desiredWidth = 200;
        short desiredHeight = 200;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (null != this.text && !"".equals(this.text.trim())) {
            int realSize = (int) this.paintTextForeground.measureText(this.text) + 60;
            if (realSize < 200) {
                realSize = 200;
            }

            if (widthMode == 1073741824) {
                width = widthSize;
            } else if (widthMode == -2147483648) {
                width = realSize;
            } else {
                width = realSize;
            }

            if (heightMode == 1073741824) {
                height = heightSize;
            } else if (heightMode == -2147483648) {
                height = realSize;
            } else {
                height = realSize;
            }
        } else {
            if (widthMode == 1073741824) {
                width = widthSize;
            } else if (widthMode == -2147483648) {
                width = Math.min(desiredWidth, widthSize);
            } else {
                width = desiredWidth;
            }

            if (heightMode == 1073741824) {
                height = heightSize;
            } else if (heightMode == -2147483648) {
                height = Math.min(desiredHeight, heightSize);
            } else {
                height = desiredHeight;
            }
        }

        this.setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int paddingLeft = this.getPaddingLeft();
        int paddingRight = this.getPaddingRight();
        int paddingTop = this.getPaddingTop();
        int paddingBottom = this.getPaddingBottom();
        int width = this.getWidth() - paddingLeft - paddingRight;
        int height = this.getHeight() - paddingTop - paddingBottom;
        int radius = Math.min(width, height) / 2;
        if (null != this.text && !"".equals(this.text.trim())) {
            this.drawText(canvas);
        } else {
            canvas.drawCircle((float) (width / 2), (float) (height / 2), (float) radius, this.mPaint);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int paddingLeft = this.getPaddingLeft();
        int paddingTop = this.getPaddingTop();
        int contentWidth = w - paddingLeft - this.getPaddingRight();
        int contentHeight = h - paddingTop - this.getPaddingBottom();
        this.radius = contentWidth < contentHeight ? contentWidth / 2 : contentHeight / 2;
        this.centerX = paddingLeft + this.radius;
        this.centerY = paddingTop + this.radius;
        this.refreshTextSizeConfig();
    }

    private void refreshTextSizeConfig() {
        this.paintTextForeground.setTextSize(DensityUtils.dp2px(getContext(), this.textSizeRatio * 2.0F * 100.0F / 3f));
        this.fontMetrics = this.paintTextForeground.getFontMetrics();
    }

    static class CircleTextImageUtil {
        CircleTextImageUtil() {
        }

        private static String getRandomColor() {
            ArrayList colorList = new ArrayList();
            colorList.add("#303F9F");
            colorList.add("#FF4081");
            colorList.add("#59dbe0");
            colorList.add("#f57f68");
            colorList.add("#87d288");
            colorList.add("#f8b552");
            colorList.add("#990099");
            colorList.add("#90a4ae");
            colorList.add("#7baaf7");
            colorList.add("#4dd0e1");
            colorList.add("#4db6ac");
            colorList.add("#aed581");
            colorList.add("#fdd835");
            colorList.add("#f2a600");
            colorList.add("#ff8a65");
            colorList.add("#f48fb1");
            colorList.add("#7986cb");
            colorList.add("#FFFFE0");
            colorList.add("#ADD8E6");
            colorList.add("#DEB887");
            colorList.add("#C0C0C0");
            colorList.add("#AFEEEE");
            colorList.add("#F0FFF0");
            colorList.add("#FF69B4");
            colorList.add("#FFE4B5");
            colorList.add("#FFE4E1");
            colorList.add("#FFEBCD");
            colorList.add("#FFEFD5");
            colorList.add("#FFF0F5");
            colorList.add("#FFF5EE");
            colorList.add("#FFF8DC");
            colorList.add("#FFFACD");
            return (String) colorList.get((int) (Math.random() * (double) colorList.size()));
        }

        private static String subFirstCharacter(String str) {
            return Character.isLetter(str.charAt(0)) ? Character.toUpperCase(str.charAt(0)) + "" : str.charAt(0) + "";
        }
    }
}
